package com.sh.data.engine.web.webapi.controller.authority;

import com.sh.data.engine.application.model.dto.authority.RoleDTO;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.service.authority.RoleApplicationService;
import com.sh.data.engine.common.annotation.crypto.EncryptMethod;
import com.sh.data.engine.common.annotation.permissionCheck.PermissionCheckMethod;
import com.sh.data.engine.common.enumDefinition.RoleEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.LoggingAspect;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.authority.model.param.RoleParam;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sh.data.engine.domain.annotation.userlog.*;

import javax.validation.Valid;
import java.util.List;

import static com.sh.data.engine.common.enumDefinition.PermissionCheckEnum.EDIT_ROLE;

@Validated
@RestController
@RequestMapping("/authority/role")
@Tag(name = "系统管理-角色管理", description = "角色管理API")
public class RoleController {

    @Autowired
    private RoleApplicationService roleApplicationService;
    @Autowired
    private LoggingAspect loggingAspect;

    @PostMapping("/delRole")
    @Operation(summary = "删除角色")
    @PermissionCheckMethod(value = EDIT_ROLE)
    @LogExecution(operation = EngineOperation.DEL, business = Business.空间角色)
    public RestResponseEntity<Boolean> delRole(int roleId, Boolean execSecutityFlag) {
        return new RestResponseEntity<>(roleApplicationService.delRole(roleId, execSecutityFlag));
    }

    @GetMapping("/list")
    @Operation(summary = "空间内分页查询角色列表")
    @PermissionCheckMethod
    public RestResponseEntity<PagerResponse<RoleDTO>> list(
        @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
        @Parameter(description = "每页记录数", schema = @Schema(defaultValue = "10"))
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
        @Parameter(description = "角色名称")
        @RequestParam(value = "roleName", required = false) String roleName,
        @Parameter(description = "角色描述")
        @RequestParam(value = "roleDesc", required = false) String roleDesc
    ) {
        var result = roleApplicationService.list(pageNum, pageSize, roleName, roleDesc);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listSysRole")
    @Operation(summary = "系统管理分页查询角色列表")
    @PermissionCheckMethod(roles = {RoleEnum.SUPER_ADMIN})
    public RestResponseEntity<PagerResponse<RoleDTO>> listSysRole(
        @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
        @Parameter(description = "每页记录数", schema = @Schema(defaultValue = "10"))
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
        @Parameter(description = "角色名称")
        @RequestParam(value = "roleName", required = false) String roleName,
        @Parameter(description = "角色描述")
        @RequestParam(value = "roleDesc", required = false) String roleDesc
    ) {
        var result = roleApplicationService.listSysRole(pageNum, pageSize, roleName, roleDesc);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/findByMenuId")
    @Operation(summary = "查询包含某菜单的角色")
    @PermissionCheckMethod
    public RestResponseEntity<List<RoleDTO>> findByMenuId(
        @RequestParam("menuId")
        Long menuId) {
        var result = roleApplicationService.findByMenuId(menuId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getByIdWithMenu")
    @Operation(summary = "根据角色ID查询角色及其菜单")
    @PermissionCheckMethod
    public RestResponseEntity<RoleDTO> getByIdWithMenu(
        @RequestParam("roleId") Long roleId) {
        var result = roleApplicationService.getByIdWithMenu(roleId);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/addRole")
    @Operation(summary = "添加角色")
    @EncryptMethod
    @PermissionCheckMethod(value = EDIT_ROLE)
    @LogExecution(operation = EngineOperation.ADD, business = Business.空间角色)
    public RestResponseEntity<Boolean> addRole(@Valid @RequestBody RoleParam roleParam) {
        var result = roleApplicationService.addRole(roleParam, false);
        return new RestResponseEntity<>(result);
    }

    @PostMapping("/editRole")
    @Operation(summary = "修改角色信息")
    @EncryptMethod
    @PermissionCheckMethod(value = EDIT_ROLE)
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.空间角色)
    public RestResponseEntity<Boolean> editRole(@Valid @RequestBody RoleParam roleParam) {
        var result = roleApplicationService.editRole(roleParam, false);
        return new RestResponseEntity<>(result);
    }
}
